using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Data.OleDb;

namespace DAOLayer
{
    public class AbStractDao
    {

        protected static string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\QuanLyThuVien.mdb";

        protected OleDbConnection connection;
        protected OleDbDataAdapter adapter;
        protected OleDbCommand command;


        public static string ConnectionString
        {
            get
            {
                return _connectionString;
            }
            set
            {
                _connectionString = value;
            }
        }

        public void connect()
        {
            connection = new OleDbConnection(ConnectionString);
            try
            {
                connection.Open();
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }

        public void disconnect()
        {
            connection.Close();
        }
        public DataTable fillDataTable(string query)
        {
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            DataTable dt = dataset.Tables[0];
            return dt;
        }

        public DataSet executeQuery_ds(string sqlString)
        {
            DataSet dataset = new DataSet();
            command = new OleDbCommand(sqlString, connection);
            
            command.Connection = this.connection;

            adapter = new OleDbDataAdapter(sqlString, connection);
            adapter.Fill(dataset);
            
            return dataset;
        }

        public IDataReader executeQuery(string sqlString)
        {
            try
            {
                command = new OleDbCommand(sqlString, connection);
                return command.ExecuteReader();
            }
            catch (Exception ex)
            {
                ex.ToString();
                return null;
                // throw(ex);

            }
            finally
            {
                
            }

        }

        public bool executeNonQuery(string sqlString)
        {
            Boolean flag = true;
            try
            {
                command = new OleDbCommand(sqlString, connection);
                command.ExecuteNonQuery();
                flag = true;
            }
            catch (Exception ex)
            {
                ex.ToString();
                flag =  false;
            }
            finally
            {
                disconnect();
            }

            return flag;
        }

        public object executeScalar(string sqlString)
        {
            command = new OleDbCommand(sqlString, connection);
            return command.ExecuteScalar();
        }
    }
}
